package com.ass.database.builder;

import com.ass.database.entity.DbDatabase;
import com.ass.database.entity.DbSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySQLBuilder extends DbBuilder{


    private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";

    public static final String DATA_BASE_MYSQL = "mysql";


    @Override
    public String getJdbcDriver() {
        return JDBC_DRIVER;
    }

    @Override
    protected Connection getConn(DbSource dataSource) {
        try{
            Class.forName(JDBC_DRIVER);
            String ip = dataSource.getSourceIp();
            String port = dataSource.getSourcePort();
            String userName = dataSource.getUserName();
            String password = dataSource.getPassword();
            String url = "jdbc:mysql://"+ip+":"+port +"?useSSL=false&useUnicode=true&useOldAliasMetadataBehavior=true";
            return DriverManager.getConnection(url,userName,password);
        }catch (SQLException | ClassNotFoundException sqlException){
            sqlException.printStackTrace();
            return null;
        }
    }

}
